package avicit.bdp.dcs.threshold.other;

import avicit.bdp.dcs.semistructure.dto.TemplateDTO;
import avicit.bdp.dcs.threshold.utils.TestUtils;
import avicit.platform6.core.rest.msg.ResponseMsg;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.util.LinkedHashMap;
import java.util.List;

import static org.junit.Assert.assertEquals;

public class TemplateTest {
    private RestTemplate restTemplate = new RestTemplate();

    private static String templateName = "template_" + System.currentTimeMillis() + ".csv";

    /**
     * 所有用例执行前准备工作
     */
    @BeforeClass
    public static void init() {
        System.out.println("用例初始化...");
        TestUtils.createFile(templateName);
    }

    /**
     * 单个测试用例运行前准备工作
     */
    @Before
    public void setUp() {
        // TODO:
    }

    /**
     * 单个测试用例运行后销毁工作
     */
    @After
    public void tearDown() {
        // TODO:
    }

    /**
     * 所有用例执行完成后准备工作
     */
    @AfterClass
    public static void destory() {
        System.out.println("用例执行完成，开始清理...");
        TestUtils.deleteFile(templateName);
    }

    /***********************************************Begin 测试用例 ***********************************************/
    @Test(timeout = 5 * 60 * 1000)
    public void testFile() throws Exception {
        // 上传模板文件
        FileSystemResource fileSystemResource = new FileSystemResource(System.getProperty("user.dir") + "/" + templateName);
        MultiValueMap<String, Object> form = new LinkedMultiValueMap<>();

        form.add("fileSeparator", ",");
        form.add("file", fileSystemResource);

        ResponseEntity<ResponseMsg> result = restTemplate.postForEntity(TestUtils.HOSTPORT + "/api/bdp/dcs/template/TemplateRest/upload",
                TestUtils.buildUploadFileHttpEntity(form), ResponseMsg.class);

        TestUtils.handleFailedRequest(result);
        assertEquals("200", result.getBody().getRetCode());

        // 保存模板数据
        TemplateDTO templateDTO = new TemplateDTO();
        JSONArray jsonArray = new JSONArray();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("name", "name");
        jsonObject.put("type", "TEXT");

        JSONObject jsonObject1 = new JSONObject();
        jsonObject1.put("name", "age");
        jsonObject1.put("type", "TEXT");

        JSONObject jsonObject2 = new JSONObject();
        jsonObject2.put("name", "work");
        jsonObject2.put("type", "TEXT");

        jsonArray.add(jsonObject);
        jsonArray.add(jsonObject1);
        jsonArray.add(jsonObject2);

        templateDTO.setColumnRule(jsonArray.toJSONString());
        templateDTO.setTemplateName(templateName);
        templateDTO.setFileType(2);
        templateDTO.setFileSeparator(",");
        templateDTO.setFileSizeLimit(1000);
        templateDTO.setLengthLimit(30);

        String param = JSONObject.toJSONString(templateDTO);
        result = restTemplate.postForEntity(TestUtils.HOSTPORT + "/api/bdp/dcs/template/TemplateRest/createTemplate",
                TestUtils.buildPostHttpEntity(param), ResponseMsg.class);

        TestUtils.handleFailedRequest(result);
        assertEquals("200", result.getBody().getRetCode());

        // 获取模板列表
        result = restTemplate.exchange(TestUtils.HOSTPORT + "/api/bdp/dcs/template/TemplateRest/getTemplateList",
                HttpMethod.GET, TestUtils.buildGetHttpEntity(), ResponseMsg.class);

        TestUtils.handleFailedRequest(result);
        assertEquals("200", result.getBody().getRetCode());

        LinkedHashMap<String, Object> map = (LinkedHashMap<String, Object>) result.getBody().getResponseBody();
        List<Object> finalBody = (List<Object>) map.get("result");
        String templateId = "";
        for (Object obj : finalBody) {
            LinkedHashMap<String, String> templateObj = (LinkedHashMap<String, String>) obj;
            TemplateDTO template = JSON.parseObject(JSON.toJSONString(templateObj), TemplateDTO.class);
            if (template.getTemplateName().equals(templateName)) {
                System.out.println("模板名称:" + template.getTemplateName());
                templateId = template.getId();
                break;
            }
        }

        // 修改模板
        templateDTO.setId(templateId);
        String param2 = JSONObject.toJSONString(templateDTO);
        result = restTemplate.postForEntity(TestUtils.HOSTPORT + "/api/bdp/dcs/template/TemplateRest/updateTemplate",
                TestUtils.buildPostHttpEntity(param2), ResponseMsg.class);

        TestUtils.handleFailedRequest(result);
        assertEquals("200", result.getBody().getRetCode());

        // 删除模板
        JSONObject param1 = new JSONObject();
        param1.put("id", templateId);
        result = restTemplate.exchange(TestUtils.HOSTPORT + "/api/bdp/dcs/template/TemplateRest/deleteTemplateByIds",
                HttpMethod.DELETE, TestUtils.buildDeleteHttpEntity(param1.toJSONString()), ResponseMsg.class);

        TestUtils.handleFailedRequest(result);
        assertEquals("200", result.getBody().getRetCode());
    }

}
